
import java.io.*;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;

public class ListAllFiles {
	public static void main(String[] args) {
		
		if( args.length != 5 ){
			System.out.println(String.join("\n",
			"cmd is 'java ListAllFiles.java dirName s1b s1e s2b s2e : as (java ListAllFiles.java D:\\svideo\\merge 0 5 5 11)'",
			"s1b :the start index of string 1",
			"s1b :the end index of string1 ",
			"s1b :the start index string 2",
			"s1b :the end index of string 2"));
			return;
		}
		
		String dirName = args[0];
		int s1b = Integer.parseInt(args[1]);
		int s1e = Integer.parseInt(args[2]);
		int s2b = Integer.parseInt(args[3]);
		int s2e = Integer.parseInt(args[4]);
				
		File dir = new File(dirName);
		System.out.println("dir------>" + dir.getAbsolutePath());
		listAll(dir,s1b,s1e,s2b,s2e);
	}

	public static void listAll(File dir,int s1b, int s1e, int s2b, int s2e) {
		File[] file_list = dir.listFiles();
		for (File file : file_list) {
			if (file.isDirectory()) {
				System.out.println("dir------>" + file.getAbsolutePath());
				listAll(file,s1b,s1e,s2b,s2e);
			} else {
				//System.out.println("file------>" + file.getAbsolutePath());
				
				String fileName = file.getAbsolutePath();
				
				String basename = file.getName();

				String subs1 = basename.substring(s1b, s1e);
				String subs2 = basename.substring(s2b, s2e);
				
				System.out.println("subs1------>" + subs1 + " subs2  --->  " + subs2);

				int num = Integer.valueOf(subs2);

				String desBase = subs1 + num + ".png";
				
				String desFile = file.getParent() + "\\" + desBase;

				System.out.println("file------>" + file.getAbsolutePath() + "   --->  " + desFile);
				
				Path source = FileSystems.getDefault().getPath(fileName);
				Path target = FileSystems.getDefault().getPath(desFile);
				
				//file------>D:\video\merge0\data_A_000974.png   --->  D:\video\merge0\data_A_974.png
				try {
					Files.move(source, target, StandardCopyOption.REPLACE_EXISTING);
				} catch (IOException e) {
					System.err.println(e);
				}
				
			}
		}
	}
}
